import Shop from '../../config/shop'
const baseUrlApi = Shop.BASE_URL
const UPLOAD_FILE = `${baseUrlApi}applet/buy/storage/common/upload` // 上传

// 上传图片
export function uploadFiles(files, formData, message, success, fail) {
  if (message != "") {
    wx.showLoading({
      title: message
    });
  }
  wx.uploadFile({
    url: UPLOAD_FILE,
    filePath: files,
    name: "file",
    formData: formData == null ? null : formData,
    success(res) {
      // const data = res.data
      if (res.statusCode == 200) {
        success(res.data);
      } else {
        let data = JSON.parse(res.data)
        wx.showToast({
          title: data.msg,
          duration: 4000,
          icon: 'none'
        })
        fail(data.msg);
      }
      // success(res.data)
      //do something
    },
    fail: function (res) {
      wx.hideNavigationBarLoading();
      if (message != "") {
        wx.hideLoading();
      }
      fail(res);
    },
    complete: function (res) { }
  }).onProgressUpdate(res => {
    wx.showToast({
      title: "已上传：" + res.progress,
      time: 5000,
      icon: "loading",
      mask: true
    });
  });
}
